from rich.console import Console
from rich.panel import Panel
from rich.style import Style
import difflib

console = Console()

def file_diff(file1, file2):
    with open(file1) as f1, open(file2) as f2:
        lines1 = f1.readlines()
        lines2 = f2.readlines()
    
    diff = difflib.ndiff(lines1, lines2)
    styled_diff = []
    for line in diff:
        if line.startswith('+ '):
            styled_diff.append(f"[green]{line[2:]}[/green]")
        elif line.startswith('- '):
            styled_diff.append(f"[red]{line[2:]}[/red]")
        else:
            styled_diff.append(f"[dim]{line[2:]}[/dim]")
    
    console.print(
        Panel(
            "\n".join(styled_diff),
            title="文件差异对比",
            border_style="bright_magenta"
        )
    )

file_diff("old.txt", "new.txt")